package com.fafi.selfmvc.controller;


import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.File;
import java.io.IOException;

@Controller
@RequestMapping("/download1")
public class DownloadController1 {
	private final static Log logger= LogFactory.getLog(DownloadController1.class);


	@GetMapping("/d2")
	public ResponseEntity<byte[]> download2(){
		String fileName = "160396245819410029.jpg";
		//获取文件对象
		try {
			byte[] bytes = FileUtils.readFileToByteArray(new File("H:\\"+fileName));
			HttpHeaders headers=new HttpHeaders();
			headers.set("Content-Disposition","attachment;filename="+fileName);
			ResponseEntity<byte[]> entity=new ResponseEntity<>(bytes,headers, HttpStatus.OK);
			return entity;
		} catch (IOException e) {
			logger.error("下载出错:",e);
			return null;
		}
	}
}
